package com.xxx.blog.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.blog.domain.ArticleType;
import com.xxx.blog.query.ArticleTypeQuery;
import com.xxx.blog.service.IArticleTypeService;
import com.xxx.blog.vo.ArticleTypeSta;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author BoWei Wang
 * @date 2025/8/5
 */

@RestController
@RequestMapping("/articleType")
@Api(tags = "文章类型接口类")
public class ArticleTypeController {
    @Autowired
    private IArticleTypeService articleTypeService;

    @GetMapping
    @ApiOperation("查询所有文章类型")
    public R<List<ArticleType>> findAll() {
        return R.ok(articleTypeService.findAll());
    }

    @GetMapping("/{id}")
    @ApiOperation("查询单个文章类型")
    public R<ArticleType> findOne(@PathVariable Long id) {
        return R.ok(articleTypeService.findOne(id));
    }

    @PostMapping
    @ApiOperation("分页条件查询文章类型")
    public PageInfo<ArticleType> queryPage(@RequestBody ArticleTypeQuery articleTypeQuery) {
        List<ArticleType> articleTypes = articleTypeService.queryPage(articleTypeQuery);
        return new PageInfo<ArticleType>(articleTypes);
    }

    @PostMapping("/add")
    @ApiOperation("添加文章类型")
    public R<Void> addArticleType(@RequestBody ArticleType articleType) {
        articleTypeService.addArticleType(articleType);
        return R.ok();
    }

    @PutMapping
    @ApiOperation("新增修改文章类型")
    public R<Void> addOrUpdateArticleType(@RequestBody ArticleType articleType) {
        try {
            if (articleType.getId() == null) {
                articleTypeService.addArticleType(articleType);
            } else {
                articleTypeService.updateArticleType(articleType);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("文章类型已存在!");
        }
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除文章类型")
    public R<Void> deleteArticleType(@PathVariable Long id) {
        try {
            articleTypeService.deleteArticleType(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @PatchMapping
    @ApiOperation("批量删除文章类型")
    public R<Void> batchDeleteArticleTypes(@RequestBody Long[] ids) {
        try {
            articleTypeService.batchDeleteArticleTypes(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @GetMapping("/treeType/{id}")
    @ApiOperation("查询类型树")
    public R<List<ArticleType>> getTypeTree(@PathVariable Long id) {

        return R.ok(articleTypeService.getTypeTree(id));
    }

    /*
    * 文章類型統計接口
    * */
    @GetMapping("/articleTypeStatistics")
    @ApiOperation("文章类型統計")
    public R<List<ArticleTypeSta>> articleTypeSta() {
        return R.ok(articleTypeService.articleTypeSta());
    }

}
